package edu.pku.sei.transformation.editor.controller.commands.link;

import edu.pku.sei.transformation.editor.model.pattern.ILinkTemplate;
import edu.pku.sei.transformation.editor.model.pattern.INodeTemplate;
import edu.pku.sei.transformation.editor.model.pattern.Pattern;

public class LinkTemplateDeleteCommand extends
		AbstractLinkDeleteCommand<ILinkTemplate, INodeTemplate, INodeTemplate> {
	private Pattern pattern;

	public LinkTemplateDeleteCommand(ILinkTemplate link, INodeTemplate source,
			INodeTemplate target) {
		super(link, source, target);
		this.pattern = link.getPattern();
	}

	@Override
	public boolean canExecute() {
		if (source != null && target != null && link != null && pattern != null) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void execute() {
		source.getOutgoing().remove(link);
		target.getIncoming().remove(link);
		pattern.getLinks().remove(link);
	}

	@Override
	public void undo() {
		this.link.setSource(this.source);
		this.link.setTarget(this.target);
		this.source.getOutgoing().add(link);
		this.target.getIncoming().add(this.link);
		pattern.getLinks().add(link);
	}
}
